VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "clsAsynch"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private WithEvents objTimer As CTimer
Attribute objTimer.VB_VarHelpID = -1
Dim iterations As Long
Dim timerID As Long

Public Event IAmWorkingOnIt(ByVal percentdone As Long, ByRef Cancel As Boolean)
Public Event TaskComplete()
Public Event TaskCancelled()


Public Sub SomeAsynchronousTask()
  iterations = 0
  objTimer.StartTimer 200
End Sub

Private Sub Class_Initialize()
  Set objTimer = New CTimer
End Sub

Private Sub Class_Terminate()
  Set objTimer = Nothing
End Sub

Private Sub objTimer_TimerEvent()
  Dim bCancel As Boolean
  iterations = iterations + 1
  If iterations > 100 Then
    RaiseEvent TaskComplete
    objTimer.StopTimer
  Else
    If iterations / 5 = Int(iterations / 5) Then
      ' Send the event every 5 iterations
      RaiseEvent IAmWorkingOnIt(iterations, bCancel)
      ' Because bCancel is declared ByRef, the client can change
      ' its value and the change will be reflected here.
      If bCancel Then
        objTimer.StopTimer
        RaiseEvent TaskCancelled    ' The task has been cancelled
      End If
    End If
  End If
End Sub

